package com.yk._12ForkJoin;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinTask;
import java.util.stream.LongStream;

public class Client {
    public static void main(String[] args) {
        Long s = 1L;
        Long e = 90_0000_0000L;
        test3(s, e);
        test2(s, e);
        test1(s, e);

    }


    public static void test3(Long s, Long e){
        long start = System.currentTimeMillis();
        long reduce = LongStream.rangeClosed(s, e).parallel().reduce(0L, Long::sum);
        long end = System.currentTimeMillis();
        System.out.println(String.valueOf(reduce)+"test3  ====   " + (end-start));
    }

    public static void test2(Long s, Long e) {
        long start = System.currentTimeMillis();
        ForkJoinPool forkJoinPool = new ForkJoinPool();
        ForkJoin01 forkJoin = new ForkJoin01(s, e);
        ForkJoinTask<Long> submit = forkJoinPool.submit(forkJoin);
        Long aLong = -1L;
        try {
            aLong = submit.get();
        } catch (Exception e1) {

            e1.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println(String.valueOf((long) aLong) + "test2  ====   " + (end - start));
    }

    public static void test1(Long s, Long e) {
        long start = System.currentTimeMillis();
        long sum = 0;
        for (long i = s; i <= e; i++) {
            sum += i;
        }
        long end = System.currentTimeMillis();
        System.out.println(sum + "test1  ====  " + (end - start));
    }
}
